package com.atm.bulksms;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.atm.bulksms.data.SMSSchedule;
import com.atm.bulksms.data.Scheduler;
import com.atm.bulksms.data.BulkSMSData.ScheduleData;
import com.atm.bulksms.util.Constants;
import com.atm.bulksms.util.Utils;

public class ScheduleActivity extends Activity{
	
	RecipientListView groupLayout ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.schedule_sms);
		groupLayout = new RecipientListView(this, R.id.selected_recipients_list);
		Button scheduleBtn = (Button)findViewById(R.id.schedule_btn);
		scheduleBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String message  = ((EditText)findViewById(R.id.schedule_message_test)).getText().toString();
				DatePicker datePicker = (DatePicker)findViewById(R.id.schedule_message_date);
				TimePicker timePicker = (TimePicker)findViewById(R.id.schedule_message_time);
				long currentTime = System.currentTimeMillis();
				Calendar cal = Calendar.getInstance();
				cal.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), timePicker.getCurrentHour(), timePicker.getCurrentMinute());
				long time = cal.getTimeInMillis();
				System.out.println("Set time "+time + " Curretn time = "+ currentTime);
				System.out.println("Selected date :: "+new Date(time));
				// validate the entered values ...
				if(TextUtils.isEmpty(message)){
					showDialog(Constants.DIALOG_MESSAGE);
					return;
				}else if(groupLayout.getCount() == 0){
					showDialog(Constants.DIALOG_MESSAGE);
					return;
				}else if(time < currentTime){
					showDialog(Constants.DIALOG_MESSAGE);
					return;
				}
				String numbers = Utils.getRecipientsDetails();
				System.out.println("Total Recipients :: "+ numbers);
				// save the values in Data base..
				ContentValues values = new ContentValues();
				values.put(ScheduleData.MESSAGE, message);
				values.put(ScheduleData.TIME,time);
				values.put(ScheduleData.NUMBERS,numbers);
				//getContentResolver().insert(ScheduleData.CONTENT_URI, values);
				// set the schedule task for sending message ... 
				Scheduler scheduler = new Scheduler(v.getContext());
				SMSSchedule schedule = new SMSSchedule(message, numbers, time);
				Message msg = new Message();
				msg.what = Scheduler.MESSAGE_SEND_SCHEDULE_SMS;
				msg.obj = schedule;
				scheduler.sendMessageDelayed(msg, time - System.currentTimeMillis());
			}
		});
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		groupLayout.loadSelectedContacts();
	}
	
	

	
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
			case Constants.DIALOG_CONTACT_PICK_LIST:
			dialog = Utils.getContactSourceDialog(this);
			break;
			case Constants.DIALOG_MESSAGE:
				dialog = Utils.createDialog(this, "Alert", "Invalid Data");
				break;
		default:
			dialog = super.onCreateDialog(id);
			break;
		}
		return dialog;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (data != null) {
				groupLayout.loadSelectedContacts();
			}
		}
	}

	public void onToggle(View v){
		groupLayout.onToggle(v);
	}

	

	
}
